﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GraphicsEngineNamespace
{
    /// <summary>
    /// Klasa odpowiedzialna za optymalizacje wyświetlania terenu przy terenie składającym się z kilku części.
    /// </summary>
    public class TerrainManager : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private Game game;
        private GameComponentCollection components;

        public GameComponentCollection Components
        {
            get { return components; }
            set { components = value; }
        }

        /// <summary>
        /// Lita list zawierająca poszczególne tereny.
        /// </summary>
        private List<List<Terrain>> terrain;

        public List<List<Terrain>> Terrain
        {
            get { return terrain; }
            set { terrain = value; }
        }

        public TerrainManager(Game game, int width, int length)
            : base(game)
        {
            this.game = game;
            Components = game.Components;

            Terrain = new List<List<Terrain>>();
            Terrain.Add(new List<Terrain>());
        }

        /// <summary>
        /// Dodanie nowego terenu do tablicy w podane współżędne tablicy.
        /// Tablica jaet to lista list, przez co może mieć dowolny rozmiar i zawierać puste pola. 
        /// Umożliwia to swobodne łączenie pól.
        /// </summary>
        /// <param name="terrain"></param>
        /// <param name="width"></param>
        /// <param name="length"></param>
        public void AddTerrain(Terrain terrain, int x, int z)
        {
            if (this.Terrain.Count <= x)
            {
                for (int i = this.Terrain.Count; i <= x; i++)
                {
                    this.Terrain.Add(new List<Terrain>());
                }
            }

            if (this.Terrain[x].Count <= z)
            {
                for (int i = this.Terrain[x].Count; i <= z; i++)
                {
                        this.Terrain[x].Add(null);
                }
            }

            if(terrain!=null)
            {
                terrain.Location = new Vector3(x, 0.0f, z);
                Components.Add(terrain);
                this.Terrain[x][z]=terrain;
            }
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        public override void Draw(GameTime gameTime)
        {
            foreach (List<Terrain> list in Terrain)
            {
                foreach (Terrain ter in list)
                {
                    if (ter != null)
                    {
                        ter.DrawTerrain(Camera.ViewMatrix, Camera.ProjectionMatrix, ter.Technique);
                    }
                }
            }
        }
    }
}